<?php

class ImagensController extends AppController {

    var $name = 'Imagens';
    var $helpers = array('Ajax', 'Javascript', 'Time', 'Cropimage');

    function index() {
        $this->Imagen->recursive = 0;
        $this->set('imagens', $this->paginate());
    }

    function upload() {
        
    }

    function createimage_step2() {
        if (!empty($this->data)) {
            $uploaded = $this->JqImgcrop->uploadImage($this->data['Imagen']['image'], '/img/upload/', 'prefix_');
            $this->set('uploaded', $uploaded);
        }
    }

    function createimage_step3() {
        $this->JqImgcrop->cropImage(151, $this->data['Imagen']['x1'], $this->data['Imagen']['y1'], $this->data['Imagen']['x2'], $this->data['Imagen']['y2'], $this->data['Imagen']['w'], $this->data['Imagen']['h'], $this->data['Imagen']['imagePath'], $this->data['Imagen']['imagePath']);
        $this->redirect(array('action' => 'index'));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Imagen inválida', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('imagen', $this->Imagen->read(null, $id));
    }

    function admin_index() {
        $this->Imagen->recursive = 0;
        $this->set('imagens', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Imagen inválida', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('imagen', $this->Imagen->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->Imagen->create();
            if ($this->Imagen->save($this->data)) {
                $this->Session->setFlash(__('La imagen ha sido guardada', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('La imagen no pudo ser guardada. Por favor, intente de nuevo.', true));
            }
        }
        $articulos = $this->Imagen->Articulo->find('list');
        $albums = $this->Imagen->Album->find('list');
        $this->set(compact('articulos', 'albums'));
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid imagen', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Imagen->save($this->data)) {
                $this->Session->setFlash(__('La imagen ha sido guardada', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('La imagen no pudo ser guardada. Por favor, intente de nuevo.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Imagen->read(null, $id);
        }
        $articulos = $this->Imagen->Articulo->find('list');
        $albums = $this->Imagen->Album->find('list');
        $this->set(compact('articulos', 'albums'));
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Id inválido para imagen', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Imagen->delete($id)) {
            $this->Session->setFlash(__('Imagen eliminada', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Imagen no fue eliminada', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>